﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace kb_24_the_game
{
    class Level3
    {
        #region Variabelen Declaratie
        private Biertje biertje;
        private ContentManager content;
        private Backgrounds scrollBackground1;
        private Player speler;
        private Obstakel lamp1;
        private IList<ISprite> allSprites;
        private SpriteFont font;
        private int score = 0;
        private Rectangle spelerRect;
        private KeyboardState state;
        #endregion Variabelen Declaratie

        public Level3(ContentManager content)
        {
            this.content = content;
            allSprites = new List<ISprite>();
            speler = new Player(content, new Vector2(50, 425)); 
            biertje = new Biertje(content, new Vector2(150, 400), 2);
            lamp1 = new Obstakel(content, 7,  new Vector2(796, 190));
            font = content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            Load();
        }

        private void Load()
        {
            scrollBackground1 = new Backgrounds(content.Load<Texture2D>("Achtergronden/BackgroundLevel03"), new Rectangle(0, 0, 3500, 600));
            allSprites.Add(scrollBackground1);
            allSprites.Add(biertje);
            allSprites.Add(speler);
        }
        public bool Update(GameTime gameTime)
        {
            spelerRect = new Rectangle((int)speler.playerPosition.X, (int)speler.playerPosition.Y, speler.texture.Width / 4, speler.texture.Height / 2);
            CheckKeys();

            CheckCollisionsMetBiertjes(spelerRect);
            biertje.CheckPositionsSpelerBiertje(speler.playerPosition, score);
            speler.Update(gameTime);
            return CheckEindCondities();
        }

        private void CheckKeys()
        {
            state = Keyboard.GetState();
            if (state.IsKeyDown(Keys.Left))
            {
                if (scrollBackground1.rectangle.X <= -1)
                {
                    scrollBackground1.Links();
                }
                if (speler.playerPosition.X >= 50)
                {
                    speler.Links();
                    biertje.beweegLinks();
                }
            }
            if (state.IsKeyDown(Keys.Right))
            {
                if (scrollBackground1.rectangle.X >= -2699)
                {
                    scrollBackground1.Rechts();
                }
                if (speler.playerPosition.X <= 500)
                {
                    speler.Rechts();
                    biertje.beweegRechts();
                }
            }
        }
        internal void Draw(SpriteBatch spriteBatch)
        {
            foreach (ISprite sprite in allSprites)
            {
                sprite.Draw(spriteBatch);
            }

            spriteBatch.DrawString(font, "Score: " + score, new Vector2(650, 25), Color.White);

        }
        private bool CheckEindCondities()
        {
            return false;
        }
        private void CheckCollisionsMetBiertjes(Rectangle spelerRect)
        {
            Rectangle bier = new Rectangle((int)biertje.position.X, (int)biertje.position.Y, biertje.texture.Width, biertje.texture.Height);
            Rectangle overlap = Rectangle.Intersect(spelerRect, bier);
            if (!overlap.IsEmpty)
            {
                score++;
                biertje.Geraakt(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
            }
        }
    }
}
